﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using GMap.NET;
using GMap.NET.WindowsForms;
using Rectangle=System.Drawing.Rectangle;

namespace LetsDoIt.Controls
{
    public class WasteMapPointMarker : GMapMarker, IDisposable
    {
        private string _pictureFile = string.Empty;
        private string _description = string.Empty;
        private int _panelWidth = 320;
        private Bitmap bmpOffline = null;
        private int rectWidth;
        private int recthHeight;
        private int stringHeight = 20;

        public WasteMapPointMarker(PointLatLng p, string description, string pictureFile, int panelWidth)
            : base(p)
        {
            _pictureFile = pictureFile;
            _description = description;
            _panelWidth = panelWidth;

            rectWidth = _panelWidth/2 - 20;
            recthHeight = 3*rectWidth/4;
            bmpOffline = new Bitmap(rectWidth - 2 * 3, recthHeight - 2 * 3);
            using (Graphics g = Graphics.FromImage(bmpOffline))
            {
                if (!string.IsNullOrEmpty(_description))
                {
                    Font fnt = new Font("Tahoma", 8, FontStyle.Regular);
                    stringHeight = (int) g.MeasureString("X", fnt).Height;
                    fnt.Dispose();
                }
                if (!string.IsNullOrEmpty(_pictureFile))
                {
                    if (File.Exists(_pictureFile))
                    {
                        ImageAttributes attr = new ImageAttributes();
                        attr.SetColorKey(Color.Transparent, Color.Transparent);
                        Bitmap bmp = new Bitmap(_pictureFile);
                        g.DrawImage(bmp,
                                    new Rectangle(0, 0, bmpOffline.Width, bmpOffline.Height),
                                    0, 0, bmp.Width, bmp.Height,
                                    GraphicsUnit.Pixel, attr);
                        bmp.Dispose();
                    }
                }
            }
        }

        public override void OnRender(Graphics g)
        {
            Pen penCross = new Pen(Color.Red, 2);
            System.Drawing.Point p1 = new System.Drawing.Point(LocalPosition.X, LocalPosition.Y);
            p1.Offset(0, -10);
            System.Drawing.Point p2 = new System.Drawing.Point(LocalPosition.X, LocalPosition.Y);
            p2.Offset(0, 10);

            System.Drawing.Point p3 = new System.Drawing.Point(LocalPosition.X, LocalPosition.Y);
            p3.Offset(-10, 0);
            System.Drawing.Point p4 = new System.Drawing.Point(LocalPosition.X, LocalPosition.Y);
            p4.Offset(10, 0);

            g.DrawLine(penCross, p1.X, p1.Y, p2.X, p2.Y);
            g.DrawLine(penCross, p3.X, p3.Y, p4.X, p4.Y);
            g.DrawEllipse(penCross, new Rectangle(LocalPosition.X - 10, LocalPosition.Y-10, 20,20));
            penCross.Dispose();


            Pen penBorder = new Pen(Color.Gray, 2);
            Brush rectFill = new SolidBrush(Color.WhiteSmoke);

            //int rectWidth = _panelWidth/2 - 20;
            //int recthHeight = 3*rectWidth/4;
            g.FillRectangle(rectFill, LocalPosition.X+2, LocalPosition.Y+2, rectWidth, recthHeight);
            g.DrawRectangle(penBorder, LocalPosition.X+2, LocalPosition.Y+2, rectWidth, recthHeight);
            //int stringHeight = 20;
            if(!string.IsNullOrEmpty(_description))
            {
                Brush descriptionBrush = new SolidBrush(Color.Black);
                Font fnt = new Font("Tahoma", 8, FontStyle.Regular);
                //stringHeight = (int)g.MeasureString("X", fnt).Height;
                g.DrawString(_description, fnt, descriptionBrush, new RectangleF(LocalPosition.X + 2 + 3, LocalPosition.Y +2 +3, rectWidth - 2 * 3, recthHeight - 2 * 3));
                fnt.Dispose();
                descriptionBrush.Dispose();
            }
            if (!string.IsNullOrEmpty(_pictureFile))
            {
                if (File.Exists(_pictureFile))
                {
                    ImageAttributes attr = new ImageAttributes();
                    attr.SetColorKey(Color.Transparent, Color.Transparent);
                    //Bitmap bmp = new Bitmap(_pictureFile);
                    g.DrawImage(bmpOffline,
                        new Rectangle(LocalPosition.X + 2 + 3, LocalPosition.Y +2 + stringHeight + 2 * 3, rectWidth - 2 * 3, recthHeight - 3 * 3 - stringHeight),
                        0, 0, bmpOffline.Width, bmpOffline.Height,
                        GraphicsUnit.Pixel, attr);
                    //bmp.Dispose();
                }
            }

            rectFill.Dispose();
            penBorder.Dispose();
        }

        #region IDisposable Members

        public void Dispose()
        {
            bmpOffline.Dispose();
        }

        #endregion
    }
}
